package com.project8.main;

import java.util.ArrayList;
import com.project8.book.Book;
import com.project8.book.Page;

import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.webkit.WebView;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class Glossary extends Activity implements OnTouchListener
{
	Book book;
	String firstTerm;
	Bundle bundle = new Bundle();
	ArrayList<TextView> allLettersLandscape;
	ArrayList<TextView> allLettersPortrait;
	String alphabet[] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
	
	TextView currentTab;					//Used in Orientation Change
	boolean inPortraitMode = true;
	
	LinearLayout landscapeLayout;
	LinearLayout portraitLayout;
	HorizontalScrollView landscapeScroll;
	ScrollView portraitScroll;
	LinearLayout landscapeLetterScroller;
	LinearLayout portraitLetterScroller;
	WebView glossaryPageLandscape;
	WebView glossaryPagePortrait;
		
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        
	    setContentView(R.layout.glossary_layout);
	                    
		bundle = getIntent().getExtras();
		book = (Book) bundle.get("Book");
		firstTerm = (String) bundle.getString("Glossary");
		
		landscapeLayout = (LinearLayout)findViewById(R.id.landscapeLayout);
		portraitLayout = (LinearLayout)findViewById(R.id.portraitLayout);
		landscapeScroll = (HorizontalScrollView)findViewById(R.id.landscapeScroll);
		portraitScroll = (ScrollView)findViewById(R.id.portraitScroll);
		landscapeLetterScroller = (LinearLayout)findViewById(R.id.letterScroll);
		portraitLetterScroller = (LinearLayout)findViewById(R.id.letterScroll2);
		glossaryPageLandscape = (WebView)findViewById(R.id.glossaryPageLandscape);
		glossaryPagePortrait = (WebView)findViewById(R.id.glossaryPagePortrait);
		
		/** Set WebView parameters for Glossary display **/
		glossaryPageLandscape.getSettings().setLightTouchEnabled(true);
		glossaryPageLandscape.getSettings().setJavaScriptEnabled(true);  
		glossaryPageLandscape.getSettings().setUseWideViewPort(true);		   //Double-Tap .. Zoom in like an image, don't just change text size
		glossaryPageLandscape.getSettings().setLoadWithOverviewMode(true);
		glossaryPageLandscape.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); 
		glossaryPageLandscape.setInitialScale(1);//Fit content to screen
		
		glossaryPageLandscape.getSettings().setLightTouchEnabled(true);
		glossaryPageLandscape.getSettings().setJavaScriptEnabled(true);  
		glossaryPageLandscape.getSettings().setUseWideViewPort(true);		   //Double-Tap .. Zoom in like an image, don't just change text size
		glossaryPageLandscape.getSettings().setLoadWithOverviewMode(true);
		glossaryPageLandscape.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); 
		glossaryPageLandscape.setInitialScale(1);//Fit content to screen
		/*************************************************/
		
		if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
		{
			//letterScroller = (LinearLayout)findViewById(R.id.letterScroll);
			landscapeLayout.setVisibility(View.VISIBLE);
			landscapeScroll.setVisibility(View.VISIBLE);
			landscapeLetterScroller.setVisibility(View.VISIBLE);
			glossaryPageLandscape.setVisibility(View.VISIBLE);
			
			portraitLayout.setVisibility(View.GONE);
			portraitScroll.setVisibility(View.GONE);
			portraitLetterScroller.setVisibility(View.GONE);
			glossaryPagePortrait.setVisibility(View.GONE);
			
			inPortraitMode = false;
		}
		else if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
		{
			//letterScroller = (LinearLayout)findViewById(R.id.letterScroll2);
			landscapeLayout.setVisibility(View.GONE);
			landscapeScroll.setVisibility(View.GONE);
			landscapeLetterScroller.setVisibility(View.GONE);
			glossaryPageLandscape.setVisibility(View.GONE);
			
			portraitLayout.setVisibility(View.VISIBLE);
			portraitScroll.setVisibility(View.VISIBLE);
			portraitLetterScroller.setVisibility(View.VISIBLE);
			glossaryPagePortrait.setVisibility(View.VISIBLE);
			
			inPortraitMode = true;
		}
						
		allLettersLandscape = new ArrayList<TextView>();
		allLettersPortrait = new ArrayList<TextView>();
		        
		for(int l = 0; l < 26; l++)
		{
			TextView letterDisplayL = new TextView(this);
			TextView letterDisplayP = new TextView(this);
			
			letterDisplayL.setBackgroundColor(Color.parseColor("#FF980000"));
			letterDisplayL.setTextColor(Color.parseColor("#FFCCCCCC"));
			
			letterDisplayP.setBackgroundColor(Color.parseColor("#FF980000"));
			letterDisplayP.setTextColor(Color.parseColor("#FFCCCCCC"));
			
			letterDisplayL.setText(alphabet[l]);
			letterDisplayL.setTextSize(22);
			letterDisplayL.setMaxLines(1);
			
			letterDisplayP.setText(alphabet[l]);
			letterDisplayP.setTextSize(22);
			letterDisplayP.setMaxLines(1);
			
			String firstLetterOfFirstTerm = "";
			if(!(firstTerm == null))
				firstLetterOfFirstTerm = firstTerm.substring(0, 1).toUpperCase();
			
			if(l == 0)
			{
				letterDisplayL.setBackgroundColor(Color.parseColor("#FFCC0000"));
				letterDisplayL.setTextColor(Color.parseColor("#FFFFFFFF"));
				
				letterDisplayP.setBackgroundColor(Color.parseColor("#FFCC0000"));
				letterDisplayP.setTextColor(Color.parseColor("#FFFFFFFF"));
				
				if(inPortraitMode)
					currentTab = letterDisplayP;
				else
					currentTab = letterDisplayP;
			}
			if(firstLetterOfFirstTerm.equals(alphabet[l]))
			{
				letterDisplayL.setBackgroundColor(Color.parseColor("#FFCC0000"));
				letterDisplayL.setTextColor(Color.parseColor("#FFFFFFFF"));
				
				letterDisplayP.setBackgroundColor(Color.parseColor("#FFCC0000"));
				letterDisplayP.setTextColor(Color.parseColor("#FFFFFFFF"));
				
				if(inPortraitMode)
					currentTab = letterDisplayP;
				else
					currentTab = letterDisplayP;
			}
			
			LinearLayout.LayoutParams layoutParams;
			
			letterDisplayL.setPadding(10, 5, 10, 5);
			layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
			layoutParams.setMargins(0, 0, 3, 0);
			letterDisplayL.setLayoutParams(layoutParams);
			
			letterDisplayP.setPadding(10, 5, 10, 5);
			layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
			layoutParams.setMargins(0, 0, 0, 3);
			letterDisplayP.setGravity(Gravity.CENTER);
			letterDisplayP.setLayoutParams(layoutParams);
			
	        letterDisplayL.setOnTouchListener((OnTouchListener) this);
	        letterDisplayP.setOnTouchListener((OnTouchListener) this);
	        
	        allLettersLandscape.add(letterDisplayL);
	        allLettersPortrait.add(letterDisplayP);

			landscapeLetterScroller.addView(letterDisplayL);
			portraitLetterScroller.addView(letterDisplayP);
		}
		
		
		if(firstTerm == null)
		{
			glossaryPageLandscape.loadUrl("file:///" + Environment.getExternalStorageDirectory() + "/Textbooks/" + book.getBookTitle() + "/Glossary/glossary_A.html");
			glossaryPagePortrait.loadUrl("file:///" + Environment.getExternalStorageDirectory() + "/Textbooks/" + book.getBookTitle() + "/Glossary/glossary_A.html");
		}
		else
		{
	        allLettersLandscape.get(0).setBackgroundColor(Color.parseColor("#FF980000"));
	        allLettersLandscape.get(0).setTextColor(Color.parseColor("#FFCCCCCC"));
	        allLettersPortrait.get(0).setBackgroundColor(Color.parseColor("#FF980000"));
	        allLettersPortrait.get(0).setTextColor(Color.parseColor("#FFCCCCCC"));
			
			glossaryPageLandscape.loadUrl("file:///" + Environment.getExternalStorageDirectory() + "/Textbooks/" + book.getBookTitle() + "/Glossary/glossary_" + currentTab.getText() + ".html#" + firstTerm);
			glossaryPagePortrait.loadUrl("file:///" + Environment.getExternalStorageDirectory() + "/Textbooks/" + book.getBookTitle() + "/Glossary/glossary_" + currentTab.getText() + ".html#" + firstTerm);
		}
    }
    
    @Override
	public void onConfigurationChanged(Configuration newConfig) 
	{
    	super.onConfigurationChanged(newConfig);
    	
		if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
		{
			landscapeLayout.setVisibility(View.VISIBLE);
			landscapeScroll.setVisibility(View.VISIBLE);
			landscapeLetterScroller.setVisibility(View.VISIBLE);
			glossaryPageLandscape.setVisibility(View.VISIBLE);
			
			portraitLayout.setVisibility(View.GONE);
			portraitScroll.setVisibility(View.GONE);
			portraitLetterScroller.setVisibility(View.GONE);
			glossaryPagePortrait.setVisibility(View.GONE);
			
			inPortraitMode = false;
		}
		else if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
		{
			landscapeLayout.setVisibility(View.GONE);
			landscapeScroll.setVisibility(View.GONE);
			landscapeLetterScroller.setVisibility(View.GONE);
			glossaryPageLandscape.setVisibility(View.GONE);
			
			portraitLayout.setVisibility(View.VISIBLE);
			portraitScroll.setVisibility(View.VISIBLE);
			portraitLetterScroller.setVisibility(View.VISIBLE);
			glossaryPagePortrait.setVisibility(View.VISIBLE);
			
			inPortraitMode = true;
		}
			
		glossaryPageLandscape.loadUrl("file:///" + Environment.getExternalStorageDirectory() + "/Textbooks/" + book.getBookTitle() + "/Glossary/glossary_" + currentTab.getText() + ".html");
		glossaryPagePortrait.loadUrl("file:///" + Environment.getExternalStorageDirectory() + "/Textbooks/" + book.getBookTitle() + "/Glossary/glossary_" + currentTab.getText() + ".html");		
	}
    
    public boolean onTouch(View view, MotionEvent event)
    {
    	switch(event.getAction())
    	{
    		case MotionEvent.ACTION_DOWN:
    		{
    			for(int v = 0; v < allLettersLandscape.size(); v++)
    			{
    				TextView aChapter = allLettersLandscape.get(v);
    				TextView bChapter = allLettersPortrait.get(v);
    				
    				if(aChapter.equals(view) || bChapter.equals(view))
    				{
    					aChapter.setBackgroundColor(Color.parseColor("#FFCC0000"));
    					aChapter.setTextColor(Color.parseColor("#FFFFFFFF"));
    					
    					bChapter.setBackgroundColor(Color.parseColor("#FFCC0000"));
    					bChapter.setTextColor(Color.parseColor("#FFFFFFFF"));
    					
    					currentTab = (TextView)view;
    					
    					glossaryPageLandscape.loadUrl("file:///" + Environment.getExternalStorageDirectory() + "/Textbooks/" + book.getBookTitle() + "/Glossary/glossary_" + currentTab.getText() + ".html");
    					glossaryPagePortrait.loadUrl("file:///" + Environment.getExternalStorageDirectory() + "/Textbooks/" + book.getBookTitle() + "/Glossary/glossary_" + currentTab.getText() + ".html");
    				}
    				else
    				{
    					aChapter.setBackgroundColor(Color.parseColor("#FF980000"));
    					aChapter.setTextColor(Color.parseColor("#FFCCCCCC"));
    					
    					bChapter.setBackgroundColor(Color.parseColor("#FF980000"));
    					bChapter.setTextColor(Color.parseColor("#FFCCCCCC"));
    				}
    			}
    			    			
    			break;
    		}
    	}//End of Touch Event = DOWN
    	
    	return true;
    }
    //End of onTouch
    
    /** Reset Glossary when Returning to Main Activity **/
    @Override
	public boolean onKeyDown(int keyCode, KeyEvent event) 
	{
		switch(keyCode)
		{
			case KeyEvent.KEYCODE_BACK:
			{
				returnResult(null);
				break;
			}
		}
		return true;
	}
			
    
    /** Allows the Main class to jump to the appropriate book page on a double-tap in ToF **/
    public void returnResult(Page jumpPage)
    {    	    	
    	this.finish();
    }
}